package com.hongyuan.sellfitness.ui.home.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import androidx.lifecycle.ViewModelProviders;
import com.google.android.material.tabs.TabLayout;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.AppViewModelFactory;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomActivity;
import com.hongyuan.sellfitness.databinding.ActivityReservationBinding;
import com.hongyuan.sellfitness.ui.home.view_mode.ReservationViewModel;
import com.hongyuan.sellfitness.ui.home.viewpage_adapter.CourseViewPagerAdapter;
import me.tatarka.bindingcollectionadapter2.BR;

public class ReservationActivity extends CustomActivity<ActivityReservationBinding, ReservationViewModel> {

    private String os_id;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_reservation;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public ReservationViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(ReservationViewModel.class);
    }

    @Override
    public void initView() {
        super.initView();
        binding.comBack.setOnClickListener(view -> finish());

        binding.mViewPager.setAdapter(new CourseViewPagerAdapter(getSupportFragmentManager()));
        binding.tab.setupWithViewPager(binding.mViewPager);
        //初始化tab
        binding.tab.getTabAt(0).setCustomView(addTabIcon("团课",true));
        binding.tab.getTabAt(1).setCustomView(addTabIcon("私教课",false));

        binding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setTabIcon(tab.getCustomView(),true);
                if(tab == binding.tab.getTabAt(0)){
                    binding.mViewPager.setCurrentItem(0);
                }else if(tab == binding.tab.getTabAt(1)){
                    binding.mViewPager.setCurrentItem(1);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                setTabIcon(tab.getCustomView(),false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void initParam() {
        super.initParam();
        os_id = getBundle().getString("os_id");
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.clearParams().setParams("os_id",os_id);
        viewModel.requestData(Constants.GET_OFFLINE_STORE_INFO);
    }

    /****
     * 创建自定义的菜单视图
     * @param name 名字
     * @param select true 选中 false 未选中
     * @return
     */
    private View addTabIcon(String name, boolean select){
        // 自定义视图
        View newtab =  LayoutInflater.from(this).inflate(R.layout.tabitem_course,null);
        TextView tv = newtab.findViewById(R.id.tabTitle);
        if(select){
            tv.setBackgroundResource(R.drawable.shape_radius24_0000000_stroke_ef5b48);
            tv.setTextColor(getResources().getColor(R.color.theme_EF5B48));
        }else{
            tv.setBackgroundResource(R.drawable.shape_soil_transparent);
            tv.setTextColor(getResources().getColor(R.color.theme_333333));
        }
        tv.setText(name);
        return newtab;
    }

    /***
     * 设置自定义菜单视图内容
     * @param newtab 视图
     * @param select true 选中 false 未选中
     * @return
     */
    private View setTabIcon( View newtab, boolean select){
        TextView tv = newtab.findViewById(R.id.tabTitle);
        if(select){
            tv.setBackgroundResource(R.drawable.shape_radius24_0000000_stroke_ef5b48);
            tv.setTextColor(getResources().getColor(R.color.theme_EF5B48));
        }else{
            tv.setBackgroundResource(R.drawable.shape_soil_transparent);
            tv.setTextColor(getResources().getColor(R.color.theme_333333));
        }
        return newtab;
    }
}
